<template>
  <div>
    <el-card shadow="never">
      <el-row>
        <el-col :span="18">
          <header-title title="历史结算单详情汇总"></header-title>
        </el-col>
        <el-col :span="6">
          <el-button @click="getExcel" class="button" size="small">导出</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-card shadow="never">
      <el-row>
        <el-col :span="8">
          <p>
            <b>商户名称：</b>{{defaultValue(businessName,'暂无信息')}}
          </p>
        </el-col>
        <el-col :span="8">
          <p>
            <b>结算编号：</b>{{defaultValue(settleAccountOrderNo,'暂无信息')}}
          </p>
        </el-col>
        <el-col :span="8">
          <p>
            <b>结算日期：</b>{{defaultValue(createdAt,'暂无信息')}}
          </p>
        </el-col>
      </el-row>
    </el-card>
    <el-card shadow="never">
      <div>
        <el-table :data="tableData" v-loading="loading" style="width: 100%">
          <el-table-column prop="businessName" label="商户名称"></el-table-column>
          <el-table-column prop="productName" label="产品名称"></el-table-column>
          <el-table-column prop="totalPrice" label="总金额(元)">
            <template slot-scope="scope">
              {{( scope.row.totalPrice / 100).toFixed(2)}}
            </template>
          </el-table-column>
          <el-table-column prop="costPrice" label="单价(元)">
            <template slot-scope="scope">
              {{( scope.row.costPrice / 100).toFixed(2)}}
            </template>
          </el-table-column>
          <el-table-column prop="amount" label="数量"></el-table-column>
          <el-table-column prop="createdAt" label="订单时间"></el-table-column>
          <div slot="empty"><img src="../../assets/blank_tip_page/blank_tip_page.png" alt=""></div>
        </el-table>
        <el-pagination
          :page-size="perPage"
          :current-page="page"
          :total="total"
          @current-change="changePage"
          style="margin-top: 15px;"
          background
          layout="prev, pager, next"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import HeaderTitle from "@/components/HeaderTitle"
import * as commoditySettlement from "@/api/commoditySettlement.js"
import { isNil } from "@/utils"
export default {
  name: '',
  components: {
    HeaderTitle
  },
  data(){
    return {
      loading: true,
      tableData: [],
      perPage: 10, // 每页显示10条
      page: 1, // 当前页
      total: 0,
      settleAccountOrderNo: '',
      createdAt: '',
      businessName: ''
    }
  },
  mounted(){
    this.settleAccountOrderNo = this.$route.query.settleAccountOrderNo
    this.createdAt = this.$route.query.createdAt
    this.businessName = this.$route.query.businessName
    this.getSDTableData()
  },
  watch: {
    '$store.state.app.marketId'(val, oldVal){
      this.getSDTableData()
    }
  },
  methods:{
    defaultValue (value, defaultValue) {
      if (isNil(value)) {
        return defaultValue
      }
      return value
    },
    // 获取 列表
    getSDTableData() {
      commoditySettlement.getSDTableData(this.settleAccountOrderNo, this.perPage, this.page)
        .then(res => {
          this.loading = false
          this.tableData = res.data.items
          this.total = res.data.total
        })
        .catch(error => {
          console.log(error)
          this.tableData = []
          this.total = 0
        })
    },
    changePage(num){
      this.page = num
      this.getSDTableData()
    },
    // 点击导出
    getExcel(){
      commoditySettlement.getExcel(this.settleAccountOrderNo).then(res => {
        window.location.href = res.data.url
      })
      .catch(error => {
        console.log(error)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  /deep/ .el-table__empty-text{
    width: 100%;
  }
.button {
  background: #64d9d6;
  color: #fff;
  margin-left: 40px;
}
</style>

